package cn.com.request;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "RequestForwardServlet", value = "/RequestForwardServlet")
public class RequestForwardServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置响应内容类型为HTML，并使用UTF-8字符编码
        response.setContentType("text/html;charset=utf-8");
        // 将用户名"张三"存储到request对象中，以便在请求转发后可以在其他Servlet中获取
        request.setAttribute("username", "张三");
        // 获取请求调度器，用于将请求转发到ResultServlet
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ResultServlet");
        // 执行请求转发，将当前请求和响应对象传递给ResultServlet处理
        dispatcher.forward(request, response);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
